import { Logger } from "../../../engine/Logger";
import proto from "./jhaoproto.js"

export class ProtoManager {

    public static instance: ProtoManager = new ProtoManager();

    public init() {
        Logger.log("ProtoManager==JhaoProto=", proto)
    }

    encode(proto_ctor, data): Uint8Array {
        if (!data) {
            return null;
        }
        try {
            let buffer_data = proto_ctor.encode(proto_ctor.create(data)).finish();
            return buffer_data;
        } catch (error) {
            Logger.error('encode fail protoName:' + proto_ctor.name + ' data:' + JSON.stringify(data));
            return null
        }
    }


    decode(proto_ctor, data: Uint8Array) {
        if (!data) {
            return null;
        }
        try {
            return proto_ctor.decode(data);
        } catch (error) {
            Logger.error('dncode fail protoName:' + proto_ctor.name);
            return null;
        }
    }

}

export interface WsSendData {
    cmd_index: proto.JhaoProto.CmdIndex,
    en_proto_ctor: any;
    data: any;
    callback?: Function;
}